package security;

import boundary.EventManager;
import boundary.InviteManager;
import boundary.SearchManager;
import boundary.UserManager;
import entity.Event;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Named;
 
import org.primefaces.event.ScheduleEntryMoveEvent;
import org.primefaces.event.ScheduleEntryResizeEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;

/**
 *
 * @author MONESI
 */
 
@Named @RequestScoped
public class ScheduleViewSearchedUser {
    
    
    //@Inject
    //EventBean eventBean;
    
    @EJB
    private UserManager um;
    
    @EJB
    private EventManager sm;
    
    @EJB
    private SearchManager searchm;
    
    
    @EJB
    private InviteManager im;
    
    List<String> events;
    List<Date> giorni;
    List<String> listaInviti;
    List<Date> giorniInvitato;
    
    List<Event> eventi;
    List<Event> eventiInvitato;
          
    private ScheduleModel eventModel; 
    private ScheduleModel lazyEventModel;
    private ScheduleEvent event = new DefaultScheduleEvent();

    @PostConstruct
    public void init(){
        
        
        //eventModel.addEvent(new DefaultScheduleEvent(getEvent(um.getLoggedUser().getEmail(),eventBean.event.date , events.size()-1), eventBean.event.date , eventBean.event.date ));
        
        eventModel = new DefaultScheduleModel();
       
        
        
        
        days(searchm.userSearched(um.getLoggedUser().getEmail()));
        
        for(int j=0;j<giorni.size();j++){
            events(searchm.userSearched(um.getLoggedUser().getEmail()),giorni.get(j));
                   for(int x=0;x<events.size();x++){
                   eventModel.addEvent(new DefaultScheduleEvent(getEvent(searchm.userSearched(um.getLoggedUser().getEmail()), giorni.get(j), x), giorni.get(j) , giorni.get(j)));
                   }
                }
        
        daysInvited(searchm.userSearched(um.getLoggedUser().getEmail()));
        
        for(int j=0;j<giorniInvitato.size();j++){
            invitedToEvent(searchm.userSearched(um.getLoggedUser().getEmail()),giorniInvitato.get(j));
                   for(int x=0;x<listaInviti.size();x++){
                   eventModel.addEvent(new DefaultScheduleEvent(getEventAccepted(searchm.userSearched(um.getLoggedUser().getEmail()), giorniInvitato.get(j), x), giorniInvitato.get(j) , giorniInvitato.get(j) ));
                   }
                }
        
        
                       
        /*lazyEventModel = new LazyScheduleModel() {
             
            @Override
            public void loadEvents(Date start, Date end) {
                Date random = getRandomDate(start);
                addEvent(new DefaultScheduleEvent("Lazy Event 1", random, random));
                 
                random = getRandomDate(start);
                addEvent(new DefaultScheduleEvent("Lazy Event 2", random, random));
            }   
        };*/
    }
    
        
    public void invitedToEvent(String loggedUser, Date data){
        //events = em.createQuery("SELECT e.name FROM EVENTS e WHERE e.creatormail LIKE :logged AND e.date LIKE :date").setParameter("logged", loggedUser).setParameter("date", data).getResultList();
        
        listaInviti = im.AcceptedEvent(loggedUser, data);
        
    }
    
    public String getEventAccepted(String loggedUser, Date data,int i){
        //events = em.createQuery("SELECT e.name FROM EVENTS e WHERE e.creatormail LIKE :logged AND e.date LIKE :date").setParameter("logged", loggedUser).setParameter("date", data).getResultList();
        
        listaInviti = im.AcceptedEvent(loggedUser, data);        
        return listaInviti.get(i);
    }
    
    public void daysInvited(String loggedUser){
        giorniInvitato = im.giorniInvitato(loggedUser);
        //giorni = em.createQuery("SELECT e.date FROM EVENTS e WHERE e.creatormail LIKE :logged GROUP BY e.date").setParameter("logged", loggedUser).getResultList();
    }
    
    public String getEvent(String loggedUser, Date data,int i){
        //events = em.createQuery("SELECT e.name FROM EVENTS e WHERE e.creatormail LIKE :logged AND e.date LIKE :date").setParameter("logged", loggedUser).setParameter("date", data).getResultList();
        
        events = sm.eventi(loggedUser, data);        
        return events.get(i);
    }
    
    public void events(String loggedUser, Date data){
        //events = em.createQuery("SELECT e.name FROM EVENTS e WHERE e.creatormail LIKE :logged AND e.date LIKE :date").setParameter("logged", loggedUser).setParameter("date", data).getResultList();
        events = sm.eventi(loggedUser, data); 

    }
     public void days(String loggedUser){
        giorni = sm.giorni(loggedUser);
        //giorni = em.createQuery("SELECT e.date FROM EVENTS e WHERE e.creatormail LIKE :logged GROUP BY e.date").setParameter("logged", loggedUser).getResultList();
    }
     
       
   
     
    public Date getInitialDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), Calendar.FEBRUARY, calendar.get(Calendar.DATE), 0, 0, 0);
         
        return calendar.getTime();
    }
     
    public ScheduleModel getEventModel() {
        return eventModel;
    }
     
    public ScheduleModel getLazyEventModel() {
        return lazyEventModel;
    }
 
    private Calendar today() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), 0, 0, 0);
 
        return calendar;
    }
     
    private Date previousDay8Pm() {
        Calendar t = (Calendar) today().clone();
        t.set(Calendar.AM_PM, Calendar.PM);
        t.set(Calendar.DATE, t.get(Calendar.DATE) - 1);
        t.set(Calendar.HOUR, 8);
         
        return t.getTime();
    }
     
    private Date previousDay11Pm() {
        Calendar t = (Calendar) today().clone();
        t.set(Calendar.AM_PM, Calendar.PM);
        t.set(Calendar.DATE, t.get(Calendar.DATE) - 1);
        t.set(Calendar.HOUR, 11);
         
        return t.getTime();
    }
     
    private Date today1Pm() {
        Calendar t = (Calendar) today().clone();
        t.set(Calendar.AM_PM, Calendar.PM);
        t.set(Calendar.HOUR, 1);
        System.out.println(t.getTime());
         
        return t.getTime();
    }
    
    
    private Date ChoseHour(int n) {
        
        Calendar t = (Calendar) today().clone();
        t.set(Calendar.DATE, t.get(Calendar.DATE));
        t.set(Calendar.AM_PM, Calendar.PM);
        t.set(Calendar.HOUR, n-1);
         
        return t.getTime();
    }
     
    private Date theDayAfter3Pm() {
        Calendar t = (Calendar) today().clone();
        t.set(Calendar.DATE, t.get(Calendar.DATE) + 2);     
        t.set(Calendar.AM_PM, Calendar.PM);
        t.set(Calendar.HOUR, 3);
         
        return t.getTime();
    }
 
    private Date today6Pm() {
        Calendar t = (Calendar) today().clone(); 
        t.set(Calendar.AM_PM, Calendar.PM);
        t.set(Calendar.HOUR, 6);
         
        return t.getTime();
    }
     
    private Date nextDay9Am() {
        Calendar t = (Calendar) today().clone();
        t.set(Calendar.AM_PM, Calendar.AM);
        t.set(Calendar.DATE, t.get(Calendar.DATE) + 1);
        t.set(Calendar.HOUR, 9);
         
        return t.getTime();
    }
     
    private Date nextDay11Am() {
        Calendar t = (Calendar) today().clone();
        t.set(Calendar.AM_PM, Calendar.AM);
        t.set(Calendar.DATE, t.get(Calendar.DATE) + 1);
        t.set(Calendar.HOUR, 11);
         
        return t.getTime();
    }
     
    private Date fourDaysLater3pm() {
        Calendar t = (Calendar) today().clone(); 
        t.set(Calendar.AM_PM, Calendar.PM);
        t.set(Calendar.DATE, t.get(Calendar.DATE) + 4);
        t.set(Calendar.HOUR, 3);
         
        return t.getTime();
    }
     
    public ScheduleEvent getEvent() {
        return event;
    }
 
    public void setEvent(ScheduleEvent event) {
        this.event = event;
    }
     
    public void addEvent(ActionEvent actionEvent) {
        if(event.getId() == null)
            eventModel.addEvent(event);
        else
            eventModel.updateEvent(event);
         
        event = new DefaultScheduleEvent();
    }
     
    public void onEventSelect(SelectEvent selectEvent) {
        event = (ScheduleEvent) selectEvent.getObject();
    }
     
    public void onDateSelect(SelectEvent selectEvent) {
        event = new DefaultScheduleEvent("", (Date) selectEvent.getObject(), (Date) selectEvent.getObject());
    }
     
    public void onEventMove(ScheduleEntryMoveEvent event) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Event moved", "Day delta:" + event.getDayDelta() + ", Minute delta:" + event.getMinuteDelta());
         
        addMessage(message);
    }
     
    public void onEventResize(ScheduleEntryResizeEvent event) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Event resized", "Day delta:" + event.getDayDelta() + ", Minute delta:" + event.getMinuteDelta());
         
        addMessage(message);
    }
     
    private void addMessage(FacesMessage message) {
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
}

